<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\common\AdminController;

class Module extends AdminController
{
    public function index()
    {
        $module_list = array();

        $dirs = array_map('basename', glob(APP_PATH . '*', GLOB_ONLYDIR));
        foreach ($dirs as $dir) {
            $module_file = APP_PATH . $dir . '/module.php';
            if (is_file($module_file)) {
                $module_info = model('admin/module')->invoke_module_method($dir, 'module_info');
                $module_info['status'] = -1; //未安装
                $module_info['sort'] = 0;
                $module_list[$module_info['name']] = $module_info;
            }
        }

        $installed_module_list = model('admin/module')->order('sort asc,id asc')->select();
        if ($installed_module_list) {
            foreach ($installed_module_list as &$module) {
                $module_list[$module['name']] = $module->getData();
            }
        }

        ksort($module_list);

        $this->assign('module_list', $module_list);
        $this->display('module/module_list');
    }

    public function modify($id)
    {
        $module_info = model('admin/module')->findOrFail($id);

        if (request()->isPost()) {
            $module_info->acceptData($this->post_array());

            if ($module_info->save() !== false) {
                $this->clear_cache();
                $this->success('修改成功。', $this->url('index'));
            }
        }

        $this->assign('module_info', $module_info);
        $this->display('module/module_form');
    }

    public function install($name)
    {
        $module_data = model('admin/module')->invoke_module_method($name, 'module_info');

        $module_config = model('admin/module')->invoke_module_method($name, 'module_config');
        $module_data['config'] = $module_config ? json_encode($module_config, JSON_UNESCAPED_UNICODE ) : '';

        $module_data['sort'] = model('admin/module')->max('sort') + 1;
        $module_data['status'] = 1;
        $module_data['is_system'] = 0;

        $module_info = model('admin/module')->initDefault();
        $module_info->acceptData($module_data);

        if ($module_info->save()) {
            $install_file = realpath(APP_PATH . $name . '/data/install.sql');
            if ($install_file) {
                $sql_status = helper('admin/sql')->execute_sql_from_file($install_file);
                if (!$sql_status) {
                    $this->error('安装失败');
                }
            }
        }

        $this->clear_cache();
        $this->success('安装成功。', $this->url('index'));
    }

    public function uninstall($id)
    {
        $module_info = model('admin/module')->findOrFail($id);

        if ($module_info['is_system'] == 1) {
            $this->error('系统模块不允许卸载。');
        }

        if (request()->isPost()) {
            if ($module_info->delete()) {
                $uninstall_file = realpath(APP_PATH . $module_info['name'] . '/data/uninstall.sql');
                if ($uninstall_file) {
                    $sql_status = helper('admin/sql')->execute_sql_from_file($uninstall_file);
                    if (!$sql_status) {
                        $this->error('卸载失败。');
                    }
                }
            }
        }

        $this->clear_cache();
        $this->success('卸载成功。', $this->url('index'));
    }

    private function clear_cache()
    {
        config('db_module_data', null);
        config('db_config_data', null);
    }
}
